﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestForCult3D
{
	public partial class Form1 : Form
	{
		string fileURL;
		public Form1()
		{
			InitializeComponent();
			string nowURL = System.IO.Directory.GetCurrentDirectory();
			fileURL = nowURL + "\\test.co";
			textBox1.Text = fileURL;
			axCult3DP1.SRC = fileURL;
			axCult3DP1.LoadCult3D(fileURL);
		}

		public string toUnicode(string str)
		{ 
			if(str.Length<7 || str.Substring(0,7)!="UNICODE")
				return str;
			else 
				str=str.Substring(7);
			string strTemp;
			int intTemp;
			byte[] array = new byte[str.Length/2];
			for (int i = 0; i < str.Length/2; i++)
			{
				strTemp = str.Substring(i*2,2);
				intTemp = Convert.ToInt32(strTemp,16);
				array[i] = (byte)intTemp;
			}
			Encoding gbkencoding = Encoding.GetEncoding(936);
			byte[] buf2 = Encoding.Convert(gbkencoding, Encoding.Unicode, array);
			string atext = Encoding.Unicode.GetString(buf2);
			return(atext);
		}

		private void axCult3DP1_OnSendMessage(object sender, AxIECULT3DPLib._ICult3DPEvents_OnSendMessageEvent e)
		{
			string str = e.message;

			richTextBox1.Text = richTextBox1.Text + "\n" + toUnicode(str);
		}

		private void button1_Click(object sender, EventArgs e)
		{
			axCult3DP1.TriggerAction(textBox2.Text, textBox3.Text);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			axCult3DP1.LoadCult3D(fileURL);
			richTextBox1.Text = "";
		}

		private void Open_Click(object sender, EventArgs e)
		{
			openFileDialog1.ShowDialog();
		}

		private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
		{
			textBox1.Text = openFileDialog1.FileName;
			fileURL = openFileDialog1.FileName;
			axCult3DP1.LoadCult3D(openFileDialog1.FileName);
		}

		private void Clear_Click(object sender, EventArgs e)
		{
			richTextBox1.Text = "";
		}

		private void textBox3_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Enter)
			{
				button1_Click(RunDIYEvent, e);
			}
		}

		private void RunEvent_Click(object sender, EventArgs e)
		{
			axCult3DP1.TriggerEvent(textBox2.Text);
		}
	}
}
